package inet.app.view.adapter;

import inet.app.R;
import inet.app.model.object.Somo_Item;
import inet.app.model.util.SmartLog;
import inet.app.model.util.Util;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.TextView;

public class SomoAdapter extends ArrayAdapter<Somo_Item> implements Filterable {
	private List<Somo_Item> items;
	private List<Somo_Item> itemFilter;
	private Filter filter;
	private Activity context;

	public SomoAdapter(Context context, List<Somo_Item> items) {

		super(context, R.layout.somo_item, items);
		this.itemFilter = items;
		this.items = new ArrayList<Somo_Item>(items);
		this.context = (Activity) context;
		this.filter = new FilterSomo();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View v = null;
		Somo_Item item = itemFilter.get(position);
		
		if (convertView == null) {
			LayoutInflater inflater = context.getLayoutInflater();
			v = inflater.inflate(R.layout.somo_item, null);
			final ViewHolder vh = new ViewHolder();
			vh.tvname = (TextView) v.findViewById(R.id.tvnamesomo);
			vh.tvnumbers = (TextView) v.findViewById(R.id.tvnumbers);
			v.setTag(vh);
		} else {
			v = convertView;
		}

		ViewHolder holder = (ViewHolder) v.getTag();
		holder.tvname.setText(item.getName());
		holder.tvnumbers.setText(item.getNumbers());
		return v;
	}
	
	@Override
	public Filter getFilter() {
        return filter;
    }

	static class ViewHolder {
		protected TextView tvname;
		protected TextView tvnumbers;
	}
	
	public class FilterSomo extends Filter{

        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            FilterResults result = new FilterResults();
            
            // if constraint is empty return the original names
            if(constraint.length() == 0 ){
                result.values = items;
                result.count = items.size();
                return result;
            }

            List<Somo_Item> arraylist_filter = new ArrayList<Somo_Item>();
            String filterString = constraint.toString().toLowerCase();

            for (Somo_Item item : items) {
				if (Util.removeAccents(item.toString()).toLowerCase()
						.contains(Util.removeAccents(filterString.toLowerCase()))) {
					arraylist_filter.add(item);
				}
			}
            
            result.values = arraylist_filter;
            result.count = arraylist_filter.size();

            return result;
        }

        @SuppressWarnings("unchecked")
		@Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
        	itemFilter = (List<Somo_Item>) results.values;
        	SmartLog.d("filter = " + itemFilter);
            notifyDataSetChanged();
            clear();
            for(Somo_Item item:itemFilter) {
            	add(item);
            }
        }
    }
}
